//
//  TCVideoView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2019/12/17.
//  Copyright © 2019 onemeeting. All rights reserved.
//

import Foundation

@objc protocol TCVideoViewDelegate {
    func trtcVideo_onClick(view:TCVideoView,event:NSEvent)
    func trtcVideo_donbleClick(view:TCVideoView,event:NSEvent)
    func trtcVideo_mouseEnter(view:TCVideoView)
    func trtcVideo_mouseExit(view:TCVideoView)
    func trtcVideo_mute(view:TCVideoView,btn:ZHButton)
    func trtcVideo_more(view:TCVideoView,btn:ZHButton)
    func trtcVideo_handUp(view:TCVideoView,icon:KIcon)
}

class TCVideoView: NSView {
    var delegate:TCVideoViewDelegate?
    
    var data: UserInfo? {
        didSet{
            if let info = data {
                self.upText = info.nickName
                self.noVideo = !info.isVdOpen
                self.isMute = info.isMute
                self.isHandup = info.isHandUp
            }
            
        }
    }
    
    var isHover:Bool = false {
        didSet { 
            self.muneView.isHidden = isHover
            if self.isHandup == true {
                let text = isHover ? "":"放下举手"
                self.handView.upText = text
            }
        }
    }
    
    var isSelect:Bool = false {
        didSet{
            let color = isSelect ? color_green200:color_gray(120)
            let width = isSelect ? 4:1
            self.touchView.zh_border(radius: 0, borderColor: color, borderWidth: CGFloat(width))
        }
    }
    
    
    
    var noVideo:Bool = false {
        didSet {
            self.videoView.isHidden = noVideo
        }
    }
    
    var isHandup:Bool = false {
        didSet {
            self.handView.isHidden = !isHandup
        }
    }
    
    var upText:String? = "000000" {
        didSet{
            self.info.text = upText
            self.bgTitle.stringValue = upText!
            self.infoUpdate()
        }
    }
    
    var isMute:Bool = false{
        didSet {
            self.info.isMute = isMute
            self.infoUpdate()
        }
    }
    
    var isVolumeTip:Bool = false {
        didSet {
            self.info.isVolumeTip = isVolumeTip
            self.infoUpdate()
        }
    }
    
    var isNETTip:Bool = false {
        didSet {
            self.info.isNETTip = isNETTip
            self.infoUpdate()
        }
    }
    
    func infoUpdate()  {
        let width = self.info.updateConstraint()
        self.info.snp.updateConstraints { (make) in
            make.width.equalTo(width+10)
        }
    }
    
    var upMuteBtnTitle:String = "" {
        didSet {
            self.muteBtn.zh_title = upMuteBtnTitle
            let width = upMuteBtnTitle.ga_width(font: 12)
            self.muteBtn.snp.updateConstraints { (make) in
                make.width.equalTo(width+10)
            }
        }
    }
    
    var backgroundColor:NSColor = NSColor.black {
        didSet {
            self.wantsLayer = true
            self.layer?.backgroundColor = backgroundColor.cgColor
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.wantsLayer = true
    }
    
    convenience init(data:UserInfo,delegate:TCVideoViewDelegate?) {
        self.init(frame: CGRect.zero)
        self.data = data
        self.delegate = delegate
        self.setupUI()
        self.loadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func loadData() {
        guard let data = self.data else {
            return
        }
        self.upText = data.nickName
    }
    
    
    
    
    func setupUI() {
        self.addSubview(bgImageV)
        bgImageV.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(100)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-22)
        }
        
        self.addSubview(bgTitle)
        bgTitle.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.top.equalTo(bgImageV.snp.bottom).offset(5)
            make.height.equalTo(32)
        }
         
        self.addSubview(videoView)
        videoView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(touchView)
        touchView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(info)
        info.snp.makeConstraints { (make) in
            make.left.equalTo(3)
            make.bottom.equalTo(-5)
            make.height.equalTo(20)
            make.width.equalTo(20)
        }
        
        
        self.addSubview(muneView)
        muneView.snp.makeConstraints { (make) in
            make.width.centerX.equalToSuperview()
            make.height.equalTo(20)
            make.top.equalTo(5)
        }
        
        self.muneView.addSubview(moreBtn)
        moreBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.right.equalTo(-5)
            make.centerY.equalToSuperview()
        }
        
        self.muneView.addSubview(muteBtn)
        muteBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-30)
            make.height.equalTo(20)
            make.centerY.equalToSuperview()
        }
        
        self.addSubview(handView)
        handView.snp.makeConstraints { (make) in
            make.width.equalTo(24)
            make.height.equalTo(20)
            make.left.equalTo(5)
            make.top.equalTo(5)
        }
    }
    
    
    lazy var info: TCInfo = {
        let view = TCInfo.init(frame: CGRect.zero)
        view.isVolumeTip = UserInfoManager.shared.isAudioVolume!
        view.isNETTip =  UserInfoManager.shared.isNETTip!
        view.isMute = false
        return view
    }()
    
    lazy var bgTitle: NSLabel = {
        let lab = NSLabel.label(title: "...", textColor: color_gray(200), font: NSFont.systemFont(ofSize: 22))
        lab.alignment = .center
        return lab
    }()
    
    lazy var bgImageV: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.image(name:"default_avatar_inmeeting")!)
        return imageV
    }()
    
    lazy var muneView: NSView = {
        let view = NSView()
        view.isHidden = true
        return view
    }()
    
    lazy var muteBtn: ZHButton = {
        let btn = ZHButton.init()
        btn.zh_title = "静音"
        btn.zh_font = NSFont.systemFont(ofSize: 12)
        btn.zh_color = color_gray(255)
        btn.hasBorder = true
        btn.zh_cornerRadius = 2
        btn.zh_backgroundColor = color_main108
        btn.makeToFit()
        btn.target = self
        btn.action = #selector(muteBtnAction(senber:))
        return btn
    }()
    
    lazy var moreBtn: ZHButton = {
        let btn = ZHButton.init()
        btn.zh_title = "..."
        btn.zh_font = NSFont.systemFont(ofSize: 12)
        btn.zh_color = color_gray(255)
        btn.hasBorder = true
        btn.zh_cornerRadius = 2
        btn.zh_backgroundColor = color_main108
        btn.target = self
        btn.action = #selector(moreBtnAction(senber:))
        return btn
    }()
    
    lazy var handView: KIcon = {
        let view = KIcon.init(data: KIconGroup.init(title: nil, image: "icon_adc_hand"))
        view.imagePosition = .left
        view.hasClickEffect = false
        view.wantsLayer = true
        view.layer?.backgroundColor = color_main108.cgColor
        view.layer?.cornerRadius = 2
        view.layer?.masksToBounds = true
        view.isHidden = true
        view.tap = { sView in
            if self.delegate != nil {
                self.delegate?.trtcVideo_handUp(view: self, icon: sView as! KIcon)
            }
        }
        return view
    }()
    
    lazy var videoView: NSView = {
        let view = NSView()
        return view
    }()
    
    lazy var touchView: MSView = {
        let view  = MSView.init(frame: NSRect.zero)
        view.donbleClick = { kView,event in
            if self.delegate != nil {
                self.delegate?.trtcVideo_donbleClick(view: self, event: event)
            }
        }
        
        view.status = { status in
            if self.delegate != nil {
                switch status {
                case .normal:
                    self.reload()
                    self.delegate?.trtcVideo_mouseEnter(view: self)
                case .hover:
                    self.delegate?.trtcVideo_mouseExit(view: self)
                default:
                    break
                }
            }
        }
        
        view.onClick = { kView,event in
            if self.delegate != nil {
                self.delegate?.trtcVideo_onClick(view: self, event: event)
            }
        }
        return view
    }()
    
    @objc func moreBtnAction(senber:ZHButton) {
        if self.delegate != nil {
            self.delegate?.trtcVideo_more(view: self, btn: senber)
        }
    }
    
    @objc func muteBtnAction(senber:ZHButton) {
        if self.delegate != nil {
            self.delegate?.trtcVideo_mute(view: self, btn: senber)
        }
    }
    
    func reload() {
        self.upMuteBtnTitle = info.isMute ? "解除静音":"静音"
    }
    
    func setFullTop(isOK:Bool) {
        let top = isOK ? 50:5
        self.muneView.snp.updateConstraints { (make) in
            make.top.equalTo(top)
            
        }
    }
    
}


